<template>
	<view class="p-2">
		<uni-list-item title="头像" @click="onChangeAvatar">
			<view class="flex align-center" slot="right">
				<image class="user-avatar rounded-circle"
					:src="userInfo.userpic ? userInfo.userpic : '/static/default.jpg'" mode=""></image>
				<text class="iconfont icon-bianji1 ml-2"></text>
			</view>
		</uni-list-item>
		<uni-list-item title="昵称">
			<view class="flex align-center" slot="right">
				<input class="text-right" type="text" v-model="userInfo.username" />
				<text class="iconfont icon-bianji1  ml-2"></text>
			</view>
		</uni-list-item>
		<uni-list-item title="性别" @click="onChangeSex">
			<view class="flex align-center" slot="right">
				<view>{{sexText}}</view>
				<text class="iconfont icon-bianji1  ml-2"></text>
			</view>
		</uni-list-item>
		<picker mode="date" :value="userInfo.birthday" @change="bindDateChange">
			<uni-list-item title="生日">
				<view class="flex align-center" slot="right">
					<view>{{userInfo.birthday}}</view>
					<text class="iconfont icon-bianji1  ml-2"></text>
				</view>
			</uni-list-item>
		</picker>
		<uni-list-item title="情感" @click="onChangeEmotion">
			<view class="flex align-center" slot="right">
				<view>{{emotionText}}</view>
				<text class="iconfont icon-bianji1  ml-2"></text>
			</view>
		</uni-list-item>
		<uni-list-item title="职业">
			<view class="flex align-center" slot="right">
				<input class="text-right" type="text" v-model="userInfo.job" />
				<text class="iconfont icon-bianji1  ml-2"></text>
			</view>
		</uni-list-item>
		<uni-list-item title="家乡" @click="onChangeHomeTown">
			<view class="flex align-center" slot="right">
				<view>{{pickerText}}</view>
				<text class="iconfont icon-bianji1  ml-2"></text>
			</view>
		</uni-list-item>
		<view class="mt-5">
			<button class="bg-main text-white rounded-circle border-0" type="default">完成</button>
		</view>
		<mpvue-city-picker :themeColor="themeColor" ref="mpvueCityPicker" :pickerValueDefault="userInfo.hometown" @onConfirm="onConfirm"></mpvue-city-picker>
	</view>
</template>

<script>
	const sexOptions = ['男', '女', '未知'];
	const emotionOptions = ['单身', '已婚', '离异', '未说明']
	import uniListItem from '@/components/uni-ui/uni-list-item/uni-list-item.vue'
	import mpvueCityPicker from '@/components/uni-ui/mpvue-citypicker/mpvueCityPicker.vue';
	export default {
		components: {
			uniListItem,
			mpvueCityPicker
		},
		data() {
			return {
				themeColor: '#007AFF',
				pickerText: '',
				userInfo: {
					userpic: '',
					username: '',
					sex: 0,
					birthday: '2002-03-05',
					emotion: 0,
					job: 'IT',
					hometown: [0, 0, 1]
				}
			};
		},
		computed: {
			sexText() {
				return sexOptions[this.userInfo.sex]
			},
			emotionText() {
				return sexOptions[this.userInfo.emotion]
			}
		},
		methods: {
			// 修改头像
			onChangeAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.userInfo.userpic = res.tempFilePaths[0];
						console.log('this.userInfo.userpic', this.userInfo.userpic)
					}
				})
			},
			// 修改性别
			onChangeSex() {
				uni.showActionSheet({
					itemList: sexOptions,
					success: (res) => {
						this.userInfo.sex = res.tapIndex;
					}
				});
			},
			// 修改情感
			onChangeEmotion() {
				uni.showActionSheet({
					itemList: emotionOptions,
					success: (res) => {
						this.userInfo.emotion = res.tapIndex;
					}
				});
			},
			// 修改生日
			bindDateChange: function(e) {
				this.userInfo.birthday = e.detail.value
			},
			onChangeHomeTown() {
				this.$refs.mpvueCityPicker.show()
			},
			// 修改城市
			onConfirm(e) {
				this.pickerText = e.label;
				this.userInfo.hometown = e.value;
			}
		}
	}
</script>

<style lang="scss">
	.user-avatar {
		width: 120rpx;
		height: 120rpx;
	}
</style>